home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 1.2 KB | 80 lines | [TEXT/CWIE] |
- // Periodic.cp
-
- #ifndef Periodic_h
- #include "Periodic.h"
- #endif
-
- Periodic::Periodic( const Method& method,
- uint32 thePeriod,
- bool startEnabled )
- : Enableable( startEnabled ),
- repeat( this, &Periodic::Repeat ),
- inProgress( false ),
- toRepeat( method ),
- period( thePeriod ),
- nextTime( 0 )
- {
- Assert( period < maxuint32 / 4 );
-
- if ( startEnabled )
- {
- nextTime = Tick::Now() + period;
- repeat.DelayTo( nextTime );
- }
- }
-
- void Periodic::BeEnabled()
- {
- nextTime = Tick::Now() + period;
-
- if ( !inProgress )
- repeat.DelayTo( nextTime );
- }
-
- void Periodic::BeDisabled()
- {
- if ( !inProgress )
- repeat.Cancel();
- }
-
- void Periodic::Repeat()
- {
- Assert( !inProgress );
- Assert( Enabled() );
-
- nextTime += period;
-
- inProgress = true;
- toRepeat();
- inProgress = false;
-
- if ( Enabled() )
- {
- Tick now( Tick::Now() );
-
- if ( nextTime < now )
- nextTime = now + period;
-
- repeat.DelayTo( nextTime );
- }
- }
-
- void Periodic::Synchronize()
- {
- Assert( Enabled() );
- nextTime = Tick::Now() + period;
-
- if ( !inProgress )
- {
- repeat.Cancel();
- repeat.DelayTo( nextTime );
- }
- }
-
- void Periodic::SetPeriod( uint32 p )
- {
- Assert( p < maxuint32 / 4 );
- period = p;
- Synchronize();
- }
-